<?php namespace App\Http\Controllers\Api;

use App\Files;
use App\Http\Controllers\Controller;
use App\Http\Services\FileManager;
use App\Http\Services\UserManager;
use App\Images;
use App\User;
use App\Userinfo;
use Illuminate\Http\Request;

class ImageController extends Controller
{
    public function upload(Request $request){
        $uploadFile = $request->file('photo');

        if(!$request->hasFile('photo'))
            return response()->json(['level'=>'error','message'=>'图片不存在']);

        if(!$uploadFile->isValid())
            return response()->json(['level'=>'error','message'=>'图片上传失败']);

        $fileName = $uploadFile->getClientOriginalName();

        /** @var Files $fileModel */
        $fileModel = FileManager::UploadFile($uploadFile,$fileName,'oss');
        $imageModel = null;
        if($fileModel->file_type == '.svg'){
            $imageModel = Images::whereFileId($fileModel->id)->first();
            if(!$imageModel){
                $imageModel = new Images();
                $imageModel->file_id = $fileModel->id;
                $imageModel->img_name= $fileName;
                $imageModel->img_type= $fileModel->file_type;
                $imageModel->save();
            }
        }else{
            $imageModel = FileManager::buildImageMultipx($fileModel);
        }

        $imageModel->img_url = FileManager::getImgUrl($imageModel->id);

        return response()->json(['level'=>'success','message'=>'图片上传成功','data'=>$imageModel]);
    }

    public function showImageById($id=0){
        if(is_numeric($id) && $id == 0)
            return \Response::download('images/default_img.png','default_img.png',[],'inline');

        /** @var Images $images */
        $images = Images::whereId($id)->first();

        if(!$images)
            return \Response::download('images/default_img.png','default_img.png',[],'inline');

        $imageFile = $images->files;
        if(!$imageFile || $imageFile == null)
            return \Response::download('images/default_img.png','default_img.png',[],'inline');

        return \Response::download($imageFile->getLocalPath(),$imageFile->img_name,[],'inline');
    }

    public function showImageByOssId($id=0){
        /** @var Images $images */
        $images = Images::whereId($id)->first();
//
//        if(!$images)
//            return \Response::download('images/default_img.png','default_img.png',[],'inline');

        $url = FileManager::getImgUrl($id);
        echo "<img src='".$url."' />";
    }

    public function uploadUserPhoto(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);

        if(!$user)
            return response()->json(["level"=>"error","message"=>"no_login","data"=>"no_login"]);

        /** @var Userinfo $userInfo */
        $userInfo = Userinfo::whereUserId($user->id)->first();

        if(!$userInfo)
            return response()->json(["level"=>"error","message"=>"用户信息不存在","data"=>""]);

        $uploadFile = $request->file('photo');

        if(!$request->hasFile('photo'))
            return response()->json(['level'=>'error','message'=>'图片不存在']);

        if(!$uploadFile->isValid())
            return response()->json(['level'=>'error','message'=>'图片上传失败']);

        $fileName = $uploadFile->getClientOriginalName();

        /** @var Files $fileModel */
        $fileModel = FileManager::UploadFile($uploadFile,$fileName,'local');
        /** @var Images $imageModel */
        $imageModel = FileManager::buildImageMultipx($fileModel);

        $userInfo->photo_id = $imageModel->id;
        $userInfo->save();

        return response()->json(['level'=>'success','message'=>'头像上传成功','data'=>(int)$imageModel->id]);
    }

    /**
     * @return \Illuminate\View\View|\Symfony\Component\HttpFoundation\Response
     */
    public function uploadBase64Image(Request $request){
        $allBase64 = $request->get('base_code');

        $imgBase64 = explode('base64,',$allBase64);

        $imgContent = base64_decode($imgBase64[1]);

        $fileMd5 = md5($imgContent);

        /** @var Files $fileModel */
        $fileModel = Files::whereFileMd5($fileMd5)->first();
        if(!$fileModel){
            $fileModel = FileManager::UploadBase64($imgContent);
        }

        /** @var Images $thatImage */
        $thatImage = Images::whereFileId($fileModel->id)->first();
        if(!$thatImage){
            $thatImage = new Images();
            $thatImage->file_id = $fileModel->id;
            $thatImage->img_name = $fileModel->file_name;
            $thatImage->img_type = $fileModel->file_type;
            $thatImage->save();
        }

        $thatImage->img_url = FileManager::getImgUrl($thatImage->id);

        return response()->json(["level"=>"success","message"=>"上传成功！","data"=>$thatImage]);
    }


    public function uploadBase64ImageFace(Request $request){
        $allBase64 = $request->get('base_code');

        $imgBase64 = explode('base64,',$allBase64);

        $imgContent = base64_decode($imgBase64[1]);

        $fileMd5 = md5($imgContent);

        /** @var Files $fileModel */
        $fileModel = Files::whereFileMd5($fileMd5)->first();
        if(!$fileModel){
            $fileModel = FileManager::UploadBase64($imgContent);
        }

        /** @var Images $thatImage */
        $thatImage = Images::whereFileId($fileModel->id)->first();
        if(!$thatImage){
            $thatImage = new Images();
            $thatImage->file_id = $fileModel->id;
            $thatImage->img_name = $fileModel->file_name;
            $thatImage->img_type = $fileModel->file_type;
            $thatImage->save();
        }

        $thatImage->img_url = FileManager::getImgUrl($thatImage->id);

        return $thatImage;
    }

    public function uploadVideo(Request $request)
    {
        $uploadFile = $request->file('video');

        if(!$request->hasFile('video'))
            return response()->json(['level'=>'error','message' => '文件不存在！'], 203);

        if(!$uploadFile->isValid())
            return response()->json(['level'=>'error','message' => '上传失败'], 203);

        $fileName = $uploadFile->getClientOriginalName();

        /** @var Files $fileModel */
        $fileModel = FileManager::uploadVideo($uploadFile,$fileName,'oss','video');

        $fileModel->video_url = FileManager::getVideoUrl($fileModel->id);

        return response()->json(['level'=>'success','message'=>'上传成功！','data'=>$fileModel]);
    }
}